'use strict';

const Service = require('egg').Service;

class UserService extends Service {
    async echo() {

    }
    //登录验证
    async login(phone, password) {
        try {

            let passwordMd5 = (password);
            console.log(passwordMd5);
            const user = await this.app.model.Users.findOne({
                where: {
                    phone
                }
            });
            // console.log(user);
            // console.log("121");
            if (user) {
                let psd = user.dataValues.password
                let usr = user.dataValues.phone
                if (phone == usr && passwordMd5 == psd) {
                    const token = this.app.jwt.sign({
                        psd:psd
                    }, this.app.config.jwt.secret);
                    return token;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (error) {
            return false
        }
    }
    //注册用户
    async createUser(body) {
        try {
            const user = {
                username: body.userName,
                phone: body.phoneNumber,
                password: body.passWord,
                // face_url: body.face_url,
            }
            await this.app.model.Users.create(user);
            return {
                "error_code": 0,
                "msg": "注册成功",

            }
            // console.log("正确1");
            // await this.app.model.Users.create(user);
            // console.log("正确2");
            // return true
        } catch (error) {
            // console.log("错误");
            return false
        }
    }

    async getUserList() {
        try {
            // console.log("11");
            return await this.app.model.Users.findAll();
        } catch (error) {
            // console.log("9999");
            return null;
        }
    }
    async deleteUser(id) {
        try {
            await this.app.model.Users.destroy({
                where: {
                    id
                }
            })
            return true
        } catch (error) {
            return false
        }
    }
    async resetPassword(id, password) {
        try {
            this.app.model.Users.update({
                password: password,
            }, {
                where: {
                    id
                }
            })
            console.log("236");
            return true
        } catch (error) {
            console.log("xxx");
            return false
        }
    }
}

module.exports = UserService;
